package cc.rengu.igas.momp.biz.service.impl;

import cc.rengu.igas.momp.common.enums.MompTxnNumEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.QueryOrgListService;
import cc.rengu.igas.momp.facade.request.QueryOrgListRequest;
import cc.rengu.igas.momp.facade.response.QueryOrgListResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;

/**
 * 营销活动列表信息查询接口实现
 *
 * @author liujinan
 * @version 1.0.0
 * @date 2021/6/28 19:26
 */
@SuppressWarnings("unchecked")
public class QueryOrgListServiceImpl implements QueryOrgListService {


    @Override
    public Result<QueryOrgListResponse> queryOrgList(QueryOrgListRequest queryOrgListRequest) {

        DubboService<QueryOrgListRequest> dubboService = new DubboServiceImpl();

        Result<QueryOrgListResponse> queryOrgListResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.QUERY_ORG_LIST.getTxnNum().equals(queryOrgListRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryOrgListResponse> dubboResult = dubboService.callAtomService(queryOrgListRequest,
                    MompTxnNumEnum.QUERY_ORG_LIST.getService(), QueryOrgListResponse.class);
            queryOrgListResponseResult.setResult(dubboResult.getResult());
            queryOrgListResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryOrgListResponse queryOrgListResponse = new QueryOrgListResponse();
            queryOrgListResponseResult.setResult(queryOrgListResponse);
            queryOrgListResponse.setHeader(queryOrgListRequest.getHeader());
            queryOrgListResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryOrgListResponse.setRespCode(((BizException) e).getCode());
                queryOrgListResponse.setRespDesc(e.getMessage());
            } else {
                queryOrgListResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryOrgListResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }
        return queryOrgListResponseResult;
    }

}
